package com.example.helloword.socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.helloword.R;

public class SocketActivity extends Activity {

	private Button socketStart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.socket_main);

		socketStart = (Button) findViewById(R.id.socketStart);

		socketStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				new Thread(new Runnable() {

					@Override
					public void run() {
						ServerSocket server = null;
						try {

							server = new ServerSocket(8888);
							Socket socket = server.accept();
							InputStream inputStream = socket.getInputStream();
							byte[] buff = new byte[1024];
							int temp = -1;
							while ((temp = inputStream.read(buff)) != -1) {

								Log.i("Test", new String(buff, 0, temp));

							}

//							server.close();

						} catch (IOException e) {

							e.printStackTrace();
						}

					}
				}).start();

			}
		});

	}
}
